﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web
{
    public partial class ListT : System.Web.UI.Page
    {
        public int[] tt;
        BLL.studentInfo s = new BLL.studentInfo();
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                string stuId = "";
                if (Session["login"] != null)
                {
                    stuId = Session["login"].ToString().Split('|')[0];
                    if (Session["login"].ToString().Split('|')[1] == "admin")
                        Response.Redirect("ListScore.aspx");
                }
                else if (Request.Cookies["stuId"] != null)
                {
                    stuId = Request.Cookies["stuId"].Value;
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript
                        (this, "alert('对不起，由于您长时间未操作，系统登录时间已过期，请重新登录！');top.location.href= '/Default.aspx '");

                }
                if (stuId != "")
                {
                    ds = s.GetPaperDetail(int.Parse(stuId));
                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        //int sn = 0, mn = 0, jn = 0;
                        //sn=int.Parse(ds.Tables[0].Rows[0][])
                        hfid.Value = stuId;
                        
                        string time = s.GetTime(int.Parse(stuId));
                        if (time != "")
                        {
                            tt = timeload(time);
                        }
                        else
                        {
                            //读取默认时间
                            tt = timeload(ds.Tables[0].Rows[0][6].ToString());
                        }
                        //[sCount] ,[mCount] ,[jCount] ,[sScore] ,[mScore] ,[jScore] 
                        lblssum.Text = ds.Tables[0].Rows[0][0].ToString();
                        lblmsum.Text = ds.Tables[0].Rows[0][1].ToString();
                        lbljsum.Text = ds.Tables[0].Rows[0][2].ToString();
                        lblsfen.Text = ds.Tables[0].Rows[0][3].ToString();
                        lblmfen.Text = ds.Tables[0].Rows[0][4].ToString();
                        lbljfen.Text = ds.Tables[0].Rows[0][5].ToString();                        
                        Bind(stuId);
                        BindAnswer(stuId);
                    }
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript
                        (this, "alert('对不起，由于您长时间未操作，系统登录时间已过期，请重新登录！');top.location.href= '/Default.aspx '");


                }

               
            }
            else
            {
                try
                {
                    string t = this.Label2.Value;
                    tt = timeload(t);
                }
                catch (Exception ex)
                {
                    string stuId = "";
                    if (Session["login"] != null)
                    {
                        stuId = Session["login"].ToString().Split('|')[0];
                    }
                    else if (Request.Cookies["stuId"] != null)
                    {
                        stuId = Request.Cookies["stuId"].Value;
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.ResponseScript
                            (this, "alert('对不起，由于您长时间未操作，系统登录时间已过期，请重新登录！');top.location.href= '/Default.aspx '");

                    }
                    //this.Label2.Value = "1:40:00";
                    ds = s.GetPaperDetail(int.Parse(stuId));
                    string time = s.GetTime(int.Parse(stuId));
                    if (time != "")
                    {
                        tt = timeload(time);
                    }
                    else
                    {
                        //读取默认时间
                        tt = timeload(ds.Tables[0].Rows[0][6].ToString());
                    }
                    tt = timeload(this.Label2.Value);
                }
                
            } 
        }
        private void Bind(string stuid)
        {
            Maticsoft.BLL.keyinfo bk = new BLL.keyinfo();
            Maticsoft.BLL.studentInfo si=new BLL.studentInfo();
            DataSet ds;
            int sid = 0;
            sid = int.Parse(stuid);
            string type=si.GetType(sid);
            if (bk.Existsbystuid(sid, 1))
            {
                ds = bk.GetList(sid, 1);
            }
            else
            {
                Maticsoft.BLL.singleInfo sm = new BLL.singleInfo();
                ds = sm.GetList(" stutype='"+type+"' order by newid()",lblssum.Text);
            }
            this.DataList1.DataSource = ds;
            this.DataList1.DataBind();
            if (bk.Existsbystuid(sid, 2))
            {
                ds = bk.GetList(sid, 2);
            }
            else
            {
                Maticsoft.BLL.mostInfo sm = new BLL.mostInfo();
                ds = sm.GetList(" stutype='" + type + "' order by newid()", lblmsum.Text);
            }
            this.DataList2.DataSource = ds;
            this.DataList2.DataBind();
            if (bk.Existsbystuid(sid, 3))
            {
                ds = bk.GetList(sid, 3);
            }
            else
            {
                Maticsoft.BLL.judgeInfo sm = new BLL.judgeInfo();
                ds = sm.GetList(" stutype='" + type + "' order by newid()", lbljsum.Text);
            }
            this.DataList3.DataSource = ds;
            this.DataList3.DataBind();
        }

        //提交答案
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string stuId = "";
            if (Session["login"] != null)
            {
                stuId = Session["login"].ToString().Split('|')[0];
            }
            else if (Request.Cookies["stuId"] != null)
            {
                stuId = Request.Cookies["stuId"].Value;
            }
            else
            {
                stuId = hfid.Value;
            }
            if (stuId != "")
            {
                Session["login"] = stuId + "|stu";
                SubmitQuestion(stuId);
                //保存时间
                BLL.studentInfo s = new BLL.studentInfo();
                s.UpdateTime(int.Parse(stuId), Label2.Value.ToString());
                Maticsoft.Common.MessageBox.Show(this, "保存答案成功！");
            }
            else
            {
                Maticsoft.Common.MessageBox.ResponseScript
                        (this, "alert('对不起，由于您长时间未操作，系统登录时间已过期，请重新登录！');top.location.href= '/Default.aspx '");
            }

        }
        protected int[] timeload(string a)
        {
            int[] t = new int[3];
            t[0] = int.Parse(a.Split(':')[0]);
            t[1] = int.Parse(a.Split(':')[1]);
            t[2] = int.Parse(a.Split(':')[2]);
            return t;
        }
        private void SubmitQuestion(string stuId)
        {
            Maticsoft.BLL.keyinfo km = new BLL.keyinfo();
            Maticsoft.Model.keyinfo k = null;

            //km.DeleteByWhere(" stuId=" + stuId + " and type = 1 ");//删除此学生的所有已提交单项选择答案
            foreach (DataListItem item in this.DataList1.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                Label lbl_singleId = (Label)item.FindControl("lbl_singleId");
                RadioButton rdoA = (RadioButton)item.FindControl("rdoA");
                RadioButton rdoB = (RadioButton)item.FindControl("rdoB");
                RadioButton rdoC = (RadioButton)item.FindControl("rdoC");
                RadioButton rdoD = (RadioButton)item.FindControl("rdoD");

                //string title = lbl_title.Text;
                string singleId = lbl_singleId.Text;//题目ID 

                string result = "";

                if (rdoA.Checked)
                {
                    result = "A";
                }
                if (rdoB.Checked)
                {
                    result = "B";
                }
                if (rdoC.Checked)
                {
                    result = "C";
                }
                if (rdoD.Checked)
                {
                    result = "D";
                }
                k = new Model.keyinfo();
                k.answer = result;
                k.tid = int.Parse(singleId);
                k.type = 1;
                k.stuId = int.Parse(stuId);
                km.Add(k);
            }
            foreach (DataListItem item in this.DataList2.Items)
            {
                Label lbl_title = (Label)item.FindControl("lbl_title");
                Label lbl_mostId = (Label)item.FindControl("lbl_mostId");

                CheckBox cboA = (CheckBox)item.FindControl("cboA");
                CheckBox cboB = (CheckBox)item.FindControl("cboB");
                CheckBox cboC = (CheckBox)item.FindControl("cboC");
                CheckBox cboD = (CheckBox)item.FindControl("cboD");

                string title = lbl_title.Text;
                string mostId = lbl_mostId.Text;//题目ID 

                string result = "";

                if (cboA.Checked)
                {
                    result += "A";
                }
                if (cboB.Checked)
                {
                    result += "B";
                }
                if (cboC.Checked)
                {
                    result += "C";
                }
                if (cboD.Checked)
                {
                    result += "D";
                }

                k = new Model.keyinfo();
                k.answer = result;
                k.tid = int.Parse(mostId);
                k.type = 2;
                k.stuId = int.Parse(stuId);

                km.Add(k);
            }
            foreach (DataListItem item in this.DataList3.Items)
            {
                Label lbl_title = (Label)item.FindControl("lbl_title");
                Label lbl_judgeId = (Label)item.FindControl("lbl_judgeId");
                RadioButton rdoYes = (RadioButton)item.FindControl("rdoYes");
                RadioButton rdoNo = (RadioButton)item.FindControl("rdoNo");

                string title = lbl_title.Text;
                string judgeId = lbl_judgeId.Text;//题目ID 

                string result = "";

                if (rdoYes.Checked)
                {
                    result = "正确";
                }
                if (rdoNo.Checked)
                {
                    result = "错误";
                }


                k = new Model.keyinfo();
                k.answer = result;
                k.tid = int.Parse(judgeId);
                k.type = 3;
                k.stuId = int.Parse(stuId);

                km.Add(k);
            }
        }
        //绑定答案
        private void BindAnswer(string stuId)
        {
            Maticsoft.BLL.keyinfo km = new BLL.keyinfo();
            List<Maticsoft.Model.keyinfo> kl = km.GetModelList(" stuId=" + stuId + " and type=1 ");
            foreach (DataListItem item in this.DataList1.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                Label lbl_singleId = (Label)item.FindControl("lbl_singleId");
                RadioButton rdoA = (RadioButton)item.FindControl("rdoA");
                RadioButton rdoB = (RadioButton)item.FindControl("rdoB");
                RadioButton rdoC = (RadioButton)item.FindControl("rdoC");
                RadioButton rdoD = (RadioButton)item.FindControl("rdoD");

                int tid = int.Parse(lbl_singleId.Text);//题目ID 
                foreach (Maticsoft.Model.keyinfo k in kl)
                {
                    if (k.tid == tid)
                    {
                        if (k.answer == "A")
                        {
                            rdoA.Checked = true;
                        }
                        if (k.answer == "B")
                        {
                            rdoB.Checked = true;
                        }
                        if (k.answer == "C")
                        {
                            rdoC.Checked = true;
                        }
                        if (k.answer == "D")
                        {
                            rdoD.Checked = true;
                        }
                    }

                }
            }
            kl = km.GetModelList(" stuId=" + stuId + " and type=2 ");

            foreach (DataListItem item in this.DataList2.Items)
            {
                Label lbl_title = (Label)item.FindControl("lbl_title");
                Label lbl_mostId = (Label)item.FindControl("lbl_mostId");

                CheckBox cboA = (CheckBox)item.FindControl("cboA");
                CheckBox cboB = (CheckBox)item.FindControl("cboB");
                CheckBox cboC = (CheckBox)item.FindControl("cboC");
                CheckBox cboD = (CheckBox)item.FindControl("cboD");

                int tid = int.Parse(lbl_mostId.Text);//题目ID 
                foreach (Maticsoft.Model.keyinfo k in kl)
                {
                    if (k.tid == tid)
                    {
                        if (k.answer.Contains("A"))
                        {
                            cboA.Checked = true;
                        }
                        if (k.answer.Contains("B"))
                        {
                            cboB.Checked = true;
                        }
                        if (k.answer.Contains("C"))
                        {
                            cboC.Checked = true;
                        }
                        if (k.answer.Contains("D"))
                        {
                            cboD.Checked = true;
                        }
                    }

                }
            }
            kl = km.GetModelList(" stuId=" + stuId + " and type=3 ");
            foreach (DataListItem item in this.DataList3.Items)
            {
                Label lbl_title = (Label)item.FindControl("lbl_title");
                Label lbl_judgeId = (Label)item.FindControl("lbl_judgeId");
                RadioButton rdoYes = (RadioButton)item.FindControl("rdoYes");
                RadioButton rdoNo = (RadioButton)item.FindControl("rdoNo");
                int tid = int.Parse(lbl_judgeId.Text);//题目ID 
                foreach (Maticsoft.Model.keyinfo k in kl)
                {
                    if (k.tid == tid)
                    {
                        if (k.answer == "正确")
                        {
                            rdoYes.Checked = true;
                        }
                        if (k.answer == "错误")
                        {
                            rdoNo.Checked = true;
                        }
                    }
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string stuId = "";
            if (Session["login"] != null)
            {
                stuId = Session["login"].ToString().Split('|')[0];
            }
            else if (Request.Cookies["stuId"] != null)
            {
                stuId = Request.Cookies["stuId"].Value;
            }
            else
            {
                stuId = hfid.Value;
            }
            if (stuId != "")
            {
                Session["login"] = stuId + "|stu";
                SubmitQuestion(stuId);
                //保存时间
                BLL.studentInfo s = new BLL.studentInfo();
                s.UpdateTime(int.Parse(stuId), Label2.Value.ToString());
                Maticsoft.Common.MessageBox.Show(this, "交卷成功！");
                Response.Redirect("exit.aspx");
            }
            else
            {
                Maticsoft.Common.MessageBox.ResponseScript
                        (this, "alert('对不起，由于您长时间未操作，系统登录时间已过期，请重新登录！');top.location.href= '/Default.aspx '");
            }
           
        }
    }
}